﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using System.Text;
using Baosight.Platform.Core.ServiceModel;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using Baosight.ZNH.ZMCommon;
using System.Windows.Browser;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCD.UI.Block;

namespace Baosight.ZNH.ZMCD.UI
{
    public partial class ZMCD02 : EFViewCmes
    {
        private EIRow FileRow;
        private DocBlock block = new DocBlock();
        public ZMCD02()
        {
            InitializeComponent();
            this.LayoutUpdated += new EventHandler(ZMCD02_LayoutUpdated);
        }


        /// <summary>
        /// 根据权限设置按钮是否可见
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ZMCD02_LayoutUpdated(object sender, EventArgs e)
        {
            if (this.Limit.Value == null)
            {
                return;
            }
            if (this.Limit.Value.Equals("0"))
            {
                Button insert = this.FindName("INSERT") as Button;
                if (insert != null)
                {
                    insert.IsEnabled = false;
                }
                Button update = this.FindName("UPDATE") as Button;
                if (update != null)
                {
                    update.IsEnabled = false;
                }
                Button delete = this.FindName("DELETE") as Button;
                if (delete != null)
                {
                    delete.IsEnabled = false;
                }
                Button move = this.FindName("MOVE") as Button;
                if (move != null)
                {
                    move.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// 重写查询方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnQueryButtonClick(object sender, RoutedEventArgs e)
        {
            this.grid.Refresh();
        }

        #region 新增 修改 删除 迁移

        /// <summary>
        /// 新增按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            EIInfo _eIInfo = new EIInfo();
            _eIInfo.AddBlock(block.GetBusinessType());
            _eIInfo.AddBlock(block.GetDocType());
            ZMCD04 zmcd04 = new ZMCD04();
            zmcd04.EIDataContext = new Platform.Core.UI.EIDataContext();
            zmcd04.EIDataContext.Update(_eIInfo);
            zmcd04.eTitle = "生产文档—新增";
            zmcd04.ModeType = OperationMode.Insert;
            zmcd04.fdCreateDate.Value = DateTime.Now.ToShortDateString();
            zmcd04.fdDirId.Value = this.fdDirId.Value;

            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = zmcd04.eTitle;//设置Title
            _win.Content = zmcd04;
            _win.Closed += CmesWindow_Closed;
            _win.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 修改按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else if (count > 1)
            {
                BaseTools.OpenEFWindowAlert("最多只能选择一条记录进行修改！", WindowAlertClosed);
                return;
            }
            else
            {
                EIInfo info = new EIInfo();
                info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCD05");
                info.Attributes.Add(EIConstants.MethodNameKey, "query");
                EIBlock block = info.AddBlock(EIConstants.QueryStatusBlock);

                EIColumn column = new EIColumn();
                column.Name = "fdForeignId";
                block.AddColumn(column);

                EIRow row1 = new EIRow();
                row1.Add("fdForeignId", EFGridCheckedRows[0]["fdKeyId"]);
                block.AddRow(row1);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(info, OnDoWorkCompleted);
            }            

        }
        /// <summary>
        /// 修改按钮中的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo _eIInfo = new EIInfo();
            IList<EIRow> _list = this.grid.GetCheckedEIRows();
            EIBlock eIBlock = _eIInfo.AddBlock("result");
            eIBlock.SetColumns(this.EIDataContext.GetBlock("result").GetColumns());
            EIRow row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list.Count > 0 ? _list[0] : new EIRow());
            eIBlock.SetRow(0, row);

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cd.engineering"));
            _eIInfo.AddBlock(block.GetBusinessType());
            _eIInfo.AddBlock(block.GetDocType());
            _eIInfo.AddBlock(e.EIInfo.GetBlock("filesresult"));
            ZMCD04 zmcd04 = new ZMCD04();
            zmcd04.EIDataContext = new Platform.Core.UI.EIDataContext();
            zmcd04.EIDataContext.Update(_eIInfo);
            zmcd04.eTitle = "生产文档—修改";
            zmcd04.ModeType = OperationMode.Update;

            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = zmcd04.eTitle;//设置Title
            _win.Content = zmcd04;
            _win.Closed += CmesWindow_Closed;
            _win.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CmesWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                base.OnQueryButtonClick(null, null);
                if (((sender as EFWindow).Content as ZMCD04) != null)
                {
                    if (((sender as EFWindow).Content as ZMCD04).ModeType.Equals(OperationMode.Update))
                    {
                        this.filesGrid.Refresh();
                    }
                }
            }

            //设置按钮可用
            this.TempButtonOpen(true);
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
            base.OnDeleteButtonClick(sender, e);
        }

        /// <summary>
        /// 迁移按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnMoveButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            List<EIRow> EFGridCheckedRows = this.grid.CheckedEIRows;
            int count = EFGridCheckedRows.Count;
            if (count <= 0)
            {
                BaseTools.OpenEFWindowAlert("请选择记录进行迁移！", WindowAlertClosed);
                return;
            }
            else
            {
                ZMCD07 zmcd07 = new ZMCD07(fdDirId.Value.ToString(), EFGridCheckedRows);
                CmesWindows _win = new CmesWindows();//构造一个弹出窗口
                _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _win.Header = zmcd07.eTitle;//设置Title
                _win.Content = zmcd07;
                _win.Closed += CmesWindow_Closed;
                _win.ShowDialog();//模态显示弹出窗口
            }
        }
        #endregion


        #region  查看明细
        /// <summary>
        /// 查看明细
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void More_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TempHyperlink = sender as HyperlinkButton;
            TempHyperlink.IsEnabled = false;
            EIRow oldRow = (TempHyperlink.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            EIInfo _eIInfo = new EIInfo();
            EIBlock eIBlock = _eIInfo.AddBlock("result");
            eIBlock.SetColumns(this.EIDataContext.GetBlock("result").GetColumns());
            eIBlock.SetRow(0, oldRow);

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cd.engineering"));
            _eIInfo.AddBlock(block.GetBusinessType());
            _eIInfo.AddBlock(block.GetDocType());
            //_eIInfo.AddBlock(this.EIDataContext.GetBlock("filesresult"));
            ZMCD04 zmcd04 = new ZMCD04();
            zmcd04.EIDataContext = new Platform.Core.UI.EIDataContext();
            zmcd04.EIDataContext.Update(_eIInfo);
            zmcd04.eTitle = "生产文档—明细";            
            zmcd04.Loaded += new RoutedEventHandler(ZMCD04_Loaded);
            zmcd04.Commit.Visibility = Visibility.Collapsed;
            zmcd04.uploadArea.Visibility = Visibility.Collapsed;
            
            CmesWindows _win = new CmesWindows();//构造一个弹出窗口
            _win.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _win.Header = zmcd04.eTitle;//设置Title
            _win.Content = zmcd04;
            _win.Closed += DetailWindow_Closed;
            _win.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 明细弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DetailWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }


        /// <summary>
        /// 当ZMCD04当做明细页面的时候，设置控件不可编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ZMCD04_Loaded(object sender, EventArgs e)
        {
            ZMCD04 zmcd04 = sender as ZMCD04;            
            BaseTools.EFDataFieldNotEnabled(zmcd04.GetChildObjects<EFDataField>(null, true));            
        }
        #endregion


        /// <summary>
        /// 选中主表一条记录，显示该条记录的从表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                this.InQu_fdForeignId.Value = e.AddedItems[0].GetType().GetProperty("fdKeyId").GetValue(e.AddedItems[0], null);
            }
            else
            {
                this.InQu_fdForeignId.Value = "0000";
            }

            if (e.AddedItems.Count > 0)
            {
                this.filesGrid.Refresh();
                return;
            }

            EIInfo eiInfo = this.BuildEIData(OperationMode.Query, eServiceName, "query", DataRange.All, null);
            eiInfo.GetBlock(EIConstants.QueryStatusBlock).GetRow(0).Add("fdForeignId", "0000", true, false);
            if (eiInfo.Blocks.ContainsKey("filesresult"))
            {
                eiInfo.RemoveBlock("filesresult");
            }
            eiInfo.AddBlock("filesresult").Set("showCount", "true");
            eiInfo.GetBlock("filesresult").Set("count", "0");
            this.filesGrid.UpdateData(eiInfo);  

        }


        #region  文件列表中下载和删除

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownLoad_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link = sender as HyperlinkButton;
            EIRow row = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();
            if (row["fdMemoryName"] != null)
            {
                Uri uri = Uploader.ConstructAbsoluteUri("../downloadpage.jsp?filename=" + HttpUtility.UrlEncode(row["fdMemoryName"].ToString()));
                HtmlPage.Window.Navigate(uri, "_blank");
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Delete_HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            TempHyperlink = sender as HyperlinkButton;
            TempHyperlink.IsEnabled = false;
            if (this.Limit.Value.Equals("1"))
            {
                HyperlinkButton link = sender as HyperlinkButton;
                FileRow = (link.ParentOfType<GridViewRow>().Item as UIBaseEntity).GetEIRow();

                BaseTools.OpenEFWindowConfirm("确定要删除该文件？", ConfrimWindowsClosed);                               
            }
            else
            {
                BaseTools.OpenEFWindowAlert("对不起，你没有对该文件的删除权限！", DetailWindow_Closed);
            }
        }

        /// <summary>
        /// 文件删除的确认框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfrimWindowsClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                //超链接生效
                if (base.TempHyperlink != null)
                {
                    base.TempHyperlink.IsEnabled = true;
                    base.TempHyperlink = null;
                }
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo _info = new EIInfo();
                _info.Attributes.Add(EIConstants.ServiceNameKey, "ZMCD05");
                _info.Attributes.Add(EIConstants.MethodNameKey, "delete");

                _info.AddBlock(EIConstants.ResultBlock).SetColumns(this.filesGrid.EIBlock.GetColumns());
                _info.GetBlock(EIConstants.ResultBlock).AddRow(FileRow);

                this.Submit(_info, FileDeleteCompleted);    //回调函数FileDeleteCompleted
            }
        }

        /// <summary>
        /// 删除文件后的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileDeleteCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status != -1)
            {
                this.filesGrid.Refresh();
            }

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }
        #endregion
    }
}
